 package com.sina.finance.api.sdk;

 import org.apache.log4j.Logger;

 import java.io.IOException;
 import java.io.UnsupportedEncodingException;
 import java.net.HttpURLConnection;
 import java.net.InetAddress;
 import java.text.SimpleDateFormat;
 import java.util.Date;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Set;
 import java.util.TimeZone;

 public class ApiLogger
 {
   private static final Logger clog = Logger.getLogger("sdk.comm.err");
   private static final Logger blog = Logger.getLogger("sdk.biz.err");

   private static String osName = System.getProperties().getProperty("os.name");
   private static String ip = null;

   public static String getIp() {
     if (ip == null) {
       try {
         ip = InetAddress.getLocalHost().getHostAddress();
       } catch (Exception localException) {
         localException.printStackTrace();
       }
     }
     return ip;
   }

   public static void setIp(String paramString) {
     ip = paramString;
   }

   public static void logCommError(Exception paramException, HttpURLConnection paramHttpURLConnection, String paramString1, String paramString2, byte[] paramArrayOfByte)
   {
     String str = null;
     try {
       str = new String(paramArrayOfByte, "UTF-8");
       logCommError(paramException, paramHttpURLConnection, paramString1, paramString2, str);
     } catch (Exception localException) {
       localException.printStackTrace();
     }
   }

   public static void logCommError(Exception paramException, String paramString1, String paramString2, String paramString3, byte[] paramArrayOfByte)
   {
     String str = null;
     try {
       str = new String(paramArrayOfByte, "UTF-8");
       logCommError(paramException, paramString1, paramString2, paramString3, str);
     } catch (UnsupportedEncodingException localUnsupportedEncodingException) {
       localUnsupportedEncodingException.printStackTrace();
     }
   }

   public static void logCommError(Exception paramException, HttpURLConnection paramHttpURLConnection, String paramString1, String paramString2, Map<String, String> paramMap)
   {
     _logCommError(paramException, paramHttpURLConnection, null, paramString1, paramString2, paramMap);
   }

   public static void logCommError(Exception paramException, String paramString1, String paramString2, String paramString3, Map<String, String> paramMap)
   {
     _logCommError(paramException, null, paramString1, paramString2, paramString3, paramMap);
   }

   private static void logCommError(Exception paramException, HttpURLConnection paramHttpURLConnection, String paramString1, String paramString2, String paramString3)
   {
     Map localMap = parseParam(paramString3);
     _logCommError(paramException, paramHttpURLConnection, null, paramString1, paramString2, localMap);
   }

   private static void logCommError(Exception paramException, String paramString1, String paramString2, String paramString3, String paramString4)
   {
     Map localMap = parseParam(paramString4);
     _logCommError(paramException, null, paramString1, paramString2, paramString3, localMap);
   }

   private static void _logCommError(Exception paramException, HttpURLConnection paramHttpURLConnection, String paramString1, String paramString2, String paramString3, Map<String, String> paramMap)
   {
     SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
     localSimpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT+8"));
     String str1 = "Rop-sdk-java-20111009";
     String str2 = null;
     String str3 = "";
     if (paramHttpURLConnection != null) {
       try {
         str2 = paramHttpURLConnection.getURL().toString();
         str3 = "HTTP_ERROR_" + paramHttpURLConnection.getResponseCode();
       } catch (IOException localIOException) {
       }
     } else {
       str2 = paramString1;
       str3 = "";
     }
     StringBuilder localStringBuilder = new StringBuilder();
     localStringBuilder.append(localSimpleDateFormat.format(new Date()));
     localStringBuilder.append("^_^");
     localStringBuilder.append(paramString3);
     localStringBuilder.append("^_^");
     localStringBuilder.append(paramString2);
     localStringBuilder.append("^_^");
     localStringBuilder.append(getIp());
     localStringBuilder.append("^_^");
     localStringBuilder.append(osName);
     localStringBuilder.append("^_^");
     localStringBuilder.append(str1);
     localStringBuilder.append("^_^");
     localStringBuilder.append(str2);
     localStringBuilder.append("^_^");
     localStringBuilder.append(str3);
     localStringBuilder.append("^_^");
     localStringBuilder.append((paramException.getMessage() + "").replaceAll("\r\n", " "));
     clog.error(localStringBuilder.toString());
   }

   private static Map<String, String> parseParam(String paramString) {
     HashMap localHashMap = new HashMap();
     if ((paramString == null) || (paramString.trim().equals(""))) {
       return localHashMap;
     }
     String[] arrayOfString1 = paramString.split("\\&");
     if (arrayOfString1 != null) {
       for (String str : arrayOfString1) {
         String[] arrayOfString3 = str.split("=");
         if ((arrayOfString3 != null) && (arrayOfString3.length == 2)) {
           localHashMap.put(arrayOfString3[0], arrayOfString3[1]);
         }
       }
     }
     return localHashMap;
   }

   public static void logBizError(String paramString)
   {
     SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
     localSimpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT+8"));
     StringBuilder localStringBuilder = new StringBuilder();
     localStringBuilder.append(localSimpleDateFormat.format(new Date()));
     localStringBuilder.append("^_^");
     localStringBuilder.append(paramString);
     blog.error(localStringBuilder.toString());
   }



   private static void appendLog(ApiHashMap paramRopHashMap, StringBuilder paramStringBuilder) {
     int i = 1;
     Set<Entry<String, String>> entries = paramRopHashMap.entrySet();
     for (Entry localEntry : entries) {
       if (i == 0)
         paramStringBuilder.append("&");
       else {
         i = 0;
       }
       paramStringBuilder.append((String)localEntry.getKey()).append("=").append((String)localEntry.getValue());
     }
   }
 }

